package com.nxk.modules.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nxk.modules.core.dao.SysRoleDao;
import com.nxk.modules.core.entity.BaseEntity;
import com.nxk.modules.core.entity.SysRole;
import com.nxk.modules.core.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {

    @Override
    @Transactional(readOnly = false)
    public boolean save(SysRole sysRole) {
        if (StringUtils.isNotBlank(sysRole.getId())) {
            return super.updateById(sysRole);
        } else {
            return super.save(sysRole);
        }
    }

    @Override
    public IPage<SysRole> page(SysRole sysRole) {
        Page<SysRole> page = new Page<SysRole>(sysRole.getPageNo(), sysRole.getPageSize());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
        if (StringUtils.isNotBlank(sysRole.getRoleCode())){
            queryWrapper.lambda().like(SysRole::getRoleCode,sysRole.getRoleCode());
        }
        if (StringUtils.isNotBlank(sysRole.getRoleName())){
            queryWrapper.lambda().like(SysRole::getRoleName,sysRole.getRoleName());
        }
        if (StringUtils.isNotBlank(sysRole.getIsSystem())){
            queryWrapper.lambda().eq(SysRole::getIsSystem,sysRole.getIsSystem());
        }
        if (StringUtils.isNotBlank(sysRole.getRoleType())){
            queryWrapper.lambda().eq(SysRole::getRoleType,sysRole.getRoleType());
        }
        //queryWrapper.setEntity(sysRole);
        queryWrapper.orderByAsc("sort").in("status",1,2);
        IPage<SysRole> result = super.page(page, queryWrapper);
        return result;
    }

    /**
     * 查询所有角色
     * @return
     */
    @Override
    public List<SysRole> ListAll() {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("status", BaseEntity.STATUS_NORMAL,BaseEntity.STATUS_STOP);
        return super.list(queryWrapper);

    }

    @Override
    @Transactional(readOnly = false)
    public boolean deleteById(String id) {
       return super.removeById(id);
    }
}
